import * as THREE from 'three';
import { LightShadow } from 'three';

class Gaffer {
    static reset(light, params) {
        // console.log('Gaffer.reset: ', ...arguments);
        let { skyColor, groundColor, intensity, hIntensity, pos, shadow } = params;
        if (!skyColor) skyColor = 0xffffff;
        if (!groundColor) groundColor = 0x000000;
        if (!pos) pos = { x: -60, y: 100, z: 0 };
        if (!intensity) intensity = 0.75;
        light.position.set(pos.x, pos.y, pos.z);
        light.color.set(skyColor);
        light.intensity = intensity;

        if (light.groundColor) light.groundColor.set(groundColor);
        if (light.shadow && light.shadow.camera && light.shadow.mapSize) {
            console.log('Gaffer.reset: ----> ', shadow);
            light.shadow.camera.far = shadow.far;
            light.shadow.mapSize.width = shadow.width;
            light.shadow.mapSize.height = shadow.height;
        }
    }
    static resetHemisphereLight(light, params) {
        console.log('Gaffer.resetHemisphereLight: ', ...arguments);
        let { skyColor, groundColor, hIntensity } = params;
        if (!skyColor) skyColor = 0xffffff;
        if (!groundColor) groundColor = 0x000000;
        if (!hIntensity) hIntensity = 0.3;
        light.color.set(skyColor);
        light.intensity = hIntensity;
        if (light.groundColor) light.groundColor.set(groundColor);
    }

    static getDirectionalLight(params) {
        let { skyColor, groundColor, intensity, pos } = params;
        if (!skyColor) skyColor = 0xffffff;
        if (!groundColor) groundColor = 0x000000;
        if (!intensity) intensity = 0.75;
        if (!pos) pos = { x: -60, y: 100, z: 0 };
        // console.log('Gaffer.getDirectionalLight: ', params, pos);
        let light = new THREE.DirectionalLight(skyColor, intensity);
        light.position.set(pos.x, pos.y, pos.z);
        light.distance = 0;

        return light;
    }
    static getSpotLight(params) {
        // console.log('Gaffer.getSpotLight: ', params);
        let { skyColor, intensity, hIntensity, pos, shadow } = params;
        if (!skyColor) skyColor = 0xffffff;
        if (!intensity) intensity = 0.75;
        if (!hIntensity) hIntensity = 0.3;
        if (!pos) pos = { x: -60, y: 100, z: 0 };
        if (!shadow) shadow = { far: pos.y * 1.5, width: 2048, height: 2048 };
        let light = new THREE.SpotLight(skyColor, intensity);
        light.name = 'SpotLight';
        // light.angle = Math.PI / 4;
        light.position.set(pos.x, pos.y, pos.z);
        // light.position.multiplyScalar(0.3);
        light.distance = 0;
        light.penumbra = 1;
        if (!shadow) return light;
        // for shadow
        light.castShadow = true;
        // light.shadowCameraVisible = true;
        light.shadow.camera.top = 50;
        light.shadow.camera.bottom = -50;
        light.shadow.camera.left = -50;
        light.shadow.camera.right = 50;
        light.shadow.camera.near = 10;
        light.shadow.camera.far = shadow.far;
        light.shadow.camera.fov = 40;
        // Value of shadow's pixels, default is 512
        light.shadow.mapSize.width = shadow.width;
        light.shadow.mapSize.height = shadow.height;

        return light;
    }
    static getHemisphereLight(params) {
        // console.log('Gaffer.getHemisphereLight: ', params);
        let { skyColor, groundColor, hIntensity } = params;
        if (!skyColor) skyColor = 0xffffff;
        if (!groundColor) groundColor = 0x000000;
        if (!hIntensity) hIntensity = 0.3;
        let light = new THREE.HemisphereLight(skyColor, groundColor, hIntensity);
        light.name = 'HemisphereLight';

        return light;
    }
}

export default Gaffer;
